package com.qdxht.dispenser.view.loading;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.text.TextUtils;
import android.view.WindowManager;

import com.qdxht.dispenser.R;

/**
 *加载中对话框
 */
public class ProgressManager {

    private ProgressDialog progressDialog=null;
    private static ProgressManager instance;
    private ProgressManager(){
    };

    public static ProgressManager getInstance(){
        if(instance==null){
            instance=new ProgressManager();
        }
        return instance;
    }

    public void showProgress(Context context, String msg, final DialogCancelListener listener, boolean cancelable){
        cancelProgress();
        progressDialog=new ProgressDialog(context);
        if(TextUtils.isEmpty(msg)){
            msg=context.getResources().getString(R.string.default_loading);
        }
        progressDialog.setMessage(msg);
        progressDialog.setCancelable(cancelable);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                 if(listener!=null&&progressDialog!=null){
                     listener.onCancel();
                 }
            }
        });
        try {
            progressDialog.show();
        }catch (WindowManager.BadTokenException e){
            e.printStackTrace();
        }
    }

    public void cancelProgress(){
        if(progressDialog!=null){
            progressDialog.dismiss();
            progressDialog=null;
        }
    }

    public interface DialogCancelListener{
        public void onCancel();
    }
}
